package com.xiaomage.hasmap_sparsearray

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.SparseArray
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    //100万
    private val MAX=1_000_00
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button1.setOnClickListener{
            val hasmap=HashMap<Int,String>()
            val start_time=System.currentTimeMillis()
            for(i in 0 until MAX){
                hasmap.put(i,i.toString())
                Log.e("进度","$i/$MAX")
            }
            val memory=Runtime.getRuntime().totalMemory()
            val end_time=System.currentTimeMillis()
            tv_hasmap.text="HasMap正序插入${MAX}条数据：" +
                    "用时：${end_time-start_time},占用内存$memory"
        }
        button2.setOnClickListener{
            val hasmap= SparseArray<String>()
            val start_time=System.currentTimeMillis()
            for(i in 0 until MAX){
                hasmap.put(i,i.toString())
                Log.e("进度","$i/$MAX")
            }
            val memory=Runtime.getRuntime().totalMemory()
            val end_time=System.currentTimeMillis()
            tv_sparearray.text="SparseArray正序插入${MAX}条数据：" +
                    "用时：${end_time-start_time},占用内存$memory"
        }

        button3.setOnClickListener{
            val hasmap=HashMap<Int,String>()
            val start_time=System.currentTimeMillis()
            for(i in MAX downTo  0){
                hasmap.put(i,i.toString())
                Log.e("进度","$i/$MAX")
            }
            val memory=Runtime.getRuntime().totalMemory()
            val end_time=System.currentTimeMillis()
            tv_hasmap.text="HasMap倒序插入${MAX}条数据：" +
                    "用时：${end_time-start_time},占用内存$memory"
        }
        button4.setOnClickListener{
            val hasmap= SparseArray<String>()
            val start_time=System.currentTimeMillis()
            for(i in MAX downTo  0){
                hasmap.put(i,i.toString())
                Log.e("进度","$i/$MAX")
            }
            val memory=Runtime.getRuntime().totalMemory()
            val end_time=System.currentTimeMillis()
            tv_sparearray.text="SparseArray倒叙插入${MAX}条数据：" +
                    "用时：${end_time-start_time},占用内存$memory"
        }

        button5.setOnClickListener{
            val hasmap=HashMap<Int,String>()
            for(i in 0 until MAX){
                hasmap.put(i,i.toString())
                Log.e("进度","put-$i/$MAX")
            }
            val start_time=System.currentTimeMillis()
            for(i in 0 until MAX){
                hasmap.get(i)
                Log.e("进度","get-$i/$MAX")
            }
            val memory=Runtime.getRuntime().totalMemory()
            val end_time=System.currentTimeMillis()
            tv_hasmap.text="HasMap查询${MAX}条数据：" +
                    "用时：${end_time-start_time},占用内存$memory"
        }

        button6.setOnClickListener{
            val hasmap=SparseArray<String>()
            for(i in 0 until MAX){
                hasmap.put(i,i.toString())
                Log.e("进度","put-$i/$MAX")
            }
            val start_time=System.currentTimeMillis()
            for(i in 0 until MAX){
                hasmap.get(i)
                Log.e("进度","get-$i/$MAX")
            }
            val memory=Runtime.getRuntime().totalMemory()
            val end_time=System.currentTimeMillis()
            tv_sparearray.text="SparseArray查询${MAX}条数据：" +
                    "用时：${end_time-start_time},占用内存$memory"
        }
    }
}

